package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Coupon;
import com.hcrazy.manchester.app.pojo.Rent;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class RentDAO extends BaseDAO<Rent>{

	public long addRent(Rent rent) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_func_rent(");
		sb.append("rentType,houseType,roomSize,hallSize,bathroomSize,zipCode,address,area,rentPrice,title,");
		sb.append("description_mc,linkName,phone,userId,publishDate,rentTime,rentTimeType,publishValidateDate,");
		sb.append("inTime,outTime,rented,theType,");
		sb.append("status,images,rentRoom,rentGender,remoteIp,praiseBaseCount,praiseCount,commentBaseCount,commentCount,pic,apartmentLayout");
		sb.append(" ) values (");
		sb.append(":rentType,:houseType,:roomSize,:hallSize,:bathroomSize,:zipCode,:address,");
		sb.append(":area,:rentPrice,:title,:description_mc,:linkName,:phone,:userId,:publishDate,:rentTime,");
		sb.append(":rentTimeType,:publishValidateDate,");
		sb.append(":inTime,:outTime,:rented,:theType,");
		sb.append(":status,:images,:rentRoom,:rentGender,:remoteIp,:praiseBaseCount,:praiseCount,:commentBaseCount,:commentCount,:pic,:apartmentLayout");
		sb.append(")");
		
		update(sb.toString(), rent);
		
		return getIncreamentId();
	}
	
	public void updateRent(Rent rent) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_func_rent set ");
		sb.append(" rentType=:rentType,houseType=:houseType,roomSize=:roomSize,hallSize=:hallSize,bathroomSize=:bathroomSize,");
		sb.append(" zipCode=:zipCode,address=:address,area=:area,rentPrice=:rentPrice,title=:title,");
		sb.append(" description_mc=:description_mc,linkName=:linkName,phone=:phone,updateDate=:updateDate,");
		sb.append(" rentTime=:rentTime,rentTimeType=:rentTimeType,publishValidateDate=:publishValidateDate,");
		sb.append(" status=:status,images=:images,rentRoom=:rentRoom,rentGender=:rentGender,");
		sb.append(" inTime=:inTime,outTime=:outTime,rented=:rented,theType=:theType,");
		sb.append(" praiseBaseCount=:praiseBaseCount,praiseCount=:praiseCount,commentBaseCount=:commentBaseCount,"
				+ "commentCount=:commentCount,pic=:pic,apartmentLayout=:apartmentLayout");
		sb.append(" where rentId = :rentId");
		
		update(sb.toString(), rent);
	}
	
	public List<Rent> listAllInfos(int theType) throws Exception{
		String sql = "select * from him_func_rent where theType = ? order by publishDate desc";
		return getList(sql, Rent.class,theType);
	}
	
	public List<Rent> getAllInfosByRentType(int theType,int rentType) throws Exception{
		String sql = "select * from him_func_rent where theType = ? and rentType = ? order by publishDate desc";
		return getList(sql, Rent.class,theType,rentType);
	}
	
	/**
	 * 后台查询租房信息功能
	 * @param model
	 * @param rentType
	 * @param houseType
	 * @param zipCode
	 * @param address
	 * @param startArea
	 * @param endArea
	 * @param startPrice
	 * @param endPrice
	 * @param title
	 * @param linkName
	 * @param phone
	 * @param startPublishDate
	 * @param endPublishDate
	 * @param status
	 * @return
	 * @throws Exception
	 */
	public PageModel getPagedInfos(PageModel model,int theType,int rentType,int houseType,
									String zipCode,String address,float startArea,float endArea,float startPrice,float endPrice,
									String title,String linkName,String phone,String startPublishDate,String endPublishDate,
									int status) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from him_func_rent");
		StringBuilder countSQL = new StringBuilder("select count(*) from him_func_rent");
		StringBuilder whereSQL = new StringBuilder(" where theType = ? ");

		List<Object> paramList = new ArrayList<Object>();
		paramList.add(theType);
		if(null != zipCode){
			whereSQL.append(" and zipCode like ? ");
			paramList.add("%"+zipCode+"%");
		}
		if(null != address){
			whereSQL.append(" and address like ? ");
			paramList.add("%"+address+"%");
		}
		if(null != title){
			whereSQL.append(" and title like ? ");
			paramList.add("%"+title+"%");
		}
		if(null != linkName){
			whereSQL.append(" and linkName like ? ");
			paramList.add("%"+linkName+"%");
		}
		if(null != phone){
			whereSQL.append(" and phone like ? ");
			paramList.add("%"+phone+"%");
		}
		
		if(rentType > 0){
			whereSQL.append(" and rentType = ?");
			paramList.add(rentType);
		}
		if(houseType > 0){
			whereSQL.append(" and houseType = ?");
			paramList.add(houseType);
		}
		if(startArea > 0 && endArea > 0){
			whereSQL.append(" and (area > ? and area < ?)");
			paramList.add(startArea);
			paramList.add(endArea);
		}
		if(startPrice > 0 && endPrice > 0){
			whereSQL.append(" and (rentPrice > ? and rentPrice < ?)");
			paramList.add(startPrice);
			paramList.add(endPrice);
		}
		if(null != startPublishDate && null != endPublishDate && (!"".equals(startPublishDate) && !"".equals(endPublishDate))){
			whereSQL.append(" and (publishDate > ? and publishDate < ?)");
			paramList.add(startPublishDate);
			paramList.add(endPublishDate);
		}
		if(status > 0){
			whereSQL.append(" and status = ?");
			paramList.add(status);
		}
//		System.out.println(querySQL.toString()+whereSQL.toString());
//		System.out.println(paramList);
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Rent.class);
	}
	
	public PageModel getPagedRents(
			PageModel model,String keywords,long userId,int theType,
			int rentType,int houseType,int status,int priceRange,
			int priceStart,int priceEnd,int roomRange) throws Exception{
		
		StringBuilder querySQL = new StringBuilder("select * from him_func_rent");
		StringBuilder countSQL = new StringBuilder("select count(*) from him_func_rent");
		StringBuilder whereSQL = new StringBuilder(" where (title like ? or description_mc like ?) and theType = ? ");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+keywords+"%");
		paramList.add("%"+keywords+"%");
		paramList.add(theType);
		
		if(rentType > 0){
			switch(rentType){
			case Constant.RENT_TYPE_ZHENGZU:
				whereSQL.append(" and rentType in (1,5,6)");
				break;
			case Constant.RENT_TYPE_HEZU:
				whereSQL.append(" and rentType in (2,7,8)");
				break;
			case Constant.RENT_TYPE_CHANGZU:
				whereSQL.append(" and rentType in (4,6,8)");
				break;
			case Constant.RENT_TYPE_DUANZU:
				whereSQL.append(" and rentType in (3,5,7)");
				break;
			default:
				break;
			}
		}
		if(houseType > 0){
			whereSQL.append(" and houseType = ?");
			paramList.add(houseType);
		}
		if(status > 0){
			whereSQL.append(" and status = ?");
			paramList.add(status);
		}
		if(priceRange > 0){
			switch(priceRange){
				case Constant.RENT_PRICE_1:
					whereSQL.append(" and rentPrice <= 500");
					break;
				case Constant.RENT_PRICE_2:
					whereSQL.append(" and rentPrice >= 500 and rentPrice <= 1000");
					break;
				case Constant.RENT_PRICE_3:
					whereSQL.append(" and rentPrice >= 1000 and rentPrice <= 1500");
					break;
				case Constant.RENT_PRICE_4:
					whereSQL.append(" and rentPrice >= 1500 and rentPrice <= 2000");
					break;
				case Constant.RENT_PRICE_5:
					whereSQL.append(" and rentPrice >= 2000 and rentPrice <= 2500");
					break;
				case Constant.RENT_PRICE_6:
					whereSQL.append(" and rentPrice >= 2500 and rentPrice <= 3000");
					break;
				case Constant.RENT_PRICE_7:
					whereSQL.append(" and rentPrice >= 3000 and rentPrice <= 3500");
					break;
				case Constant.RENT_PRICE_8:
					whereSQL.append(" and rentPrice >= 3500 and rentPrice <= 4000");
					break;
				case Constant.RENT_PRICE_9:
					whereSQL.append(" and rentPrice >= 4000");
					break;
			}
		}
		if(priceStart >= 0 && priceEnd > 0){
			whereSQL.append(" and rentPrice >= ? and rentPrice <= ?");
			paramList.add(priceStart);
			paramList.add(priceEnd);
		}
		if(roomRange > 0 ){
			switch(roomRange){
				case Constant.RENT_ROOM_RANGE_5:
					whereSQL.append(" and roomSize >= 4");
					break;
				default:
					whereSQL.append(" and roomSize = ?");
					paramList.add(roomRange);
					break;
			}
		}
		if(userId > 0){
			whereSQL.append(" and userId = ?");
			paramList.add(userId);
		}

		whereSQL.append(" order by publishDate desc");
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Rent.class);
	}
	
	public Rent getById(long rentId) throws Exception{
		String sql = "select * from him_func_rent where rentId = ?";
		return this.getJavaBean(sql, Rent.class, rentId);
	}
	
	public void delRent(long rentId) throws Exception{
		String sql = "delete from him_func_rent where rentId = ?";
		this.commonUpdate(sql, rentId);
	}
	
	public void setViews(long views,long rentId) throws Exception{
		String sql = "update him_func_rent set views = ? where rentId = ?";
		this.commonUpdate(sql, views,rentId);
	}
	
	public PageModel getCollegeRents(long userId,int moduleType,PageModel model) throws Exception{
		String sql = "select a.type,b.* from him_user_collection a,him_func_rent b where a.infoId = b.rentId";
		StringBuilder querySQL = new StringBuilder("select * from ("+sql+") as m");
		StringBuilder countSQL = new StringBuilder("select count(*) from ("+sql+") as m");
		StringBuilder whereSQL = new StringBuilder(" where userId = ? and type = ?");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(userId);
		paramList.add(moduleType);
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Rent.class);
	}

	public long getPraiseCount(long rentId) throws Exception{
		String sql = "select praiseCount from him_func_rent where rentId = ?";
		return this.getJdbcTemplate().queryForLong(sql,rentId);
	}
	
	public long getCommentCount(long rentId) throws Exception{
		String sql = "select commentCount from him_func_rent where rentId = ?";
		return this.getJdbcTemplate().queryForLong(sql,rentId);
	}
	
	public void addPraiseCount(long infoId) throws Exception {
		String sql = "update him_func_rent set praiseCount = praiseCount + 1 where rentId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public void addCommentCount(long infoId) throws Exception {
		String sql = "update him_func_rent set commentCount = commentCount + 1 where rentId = ?";
		this.commonUpdate(sql, infoId);
	}
	
	public List<Rent> getRents(int size) throws Exception{
		String sql = "select * from him_func_rent order by publishDate desc limit ?";
		
		return this.getList(sql, Rent.class,size);
	}
}
